package utils.dataComb;

import java.util.ArrayList;
import java.util.List;

public class PairCollection {

	
	private List<TestPair> pairs;

	
	public PairCollection(List<TestPair> pairs) {
		this.pairs = pairs;
	}

	
	
	public PairCollection(List<Object> inputParameterValues, List<List<Object>> inputValueLists, int paramIndex) {
	
		this.pairs = new ArrayList<>();
		
		for(Object value : inputParameterValues) {
			int paramBIndex = 0;
			for(List<Object> list : inputValueLists) {
				for (Object value2 : list) {
					pairs.add(new TestPair(paramIndex, paramBIndex, value, value2));
				}
				paramBIndex++;
			}
		}
		
	}

	
	
	public List<TestPair> getPairs() {
		return pairs;
	}
	
	
	
	public List<List<Object>> toList() {
	
		List<List<Object>> valuesList = new ArrayList<>();
		
		for(TestPair pair : pairs) {
			List<Object> pairList = new ArrayList<>();
			pairList.add(pair.getValueA());
			pairList.add(pair.getValueB());
			valuesList.add(pairList);
		}
		
		return valuesList;
		
	}

	
	
	public void removePairsCoveredBy(List<Object> combination) {
		
		List<TestPair> newPairs = new ArrayList<TestPair>(pairs);
		
		for (TestPair testPair : newPairs) {
			boolean firstValueFromPairEqualsValueFromCombination = combination.get(testPair.getValueAIndex()).equals(testPair.getValueA());
			boolean secondValueFromPairEqualsValueFromCombination = combination.get(testPair.getValueBIndex()).equals(testPair.getValueB());
			
			if(firstValueFromPairEqualsValueFromCombination && secondValueFromPairEqualsValueFromCombination) {
				pairs.remove(testPair);
			}
		}
		
	}

	
	
	public List<Object> getCombinationsThatCoversMostPairs(List<Object> combination, List<Object> inputValuesForGrowth) {
		
		List<Object> selectedInputCombination = null;
		int maxCoveredCount = 0;
		int coveredCount = 0;
		
		for(Object inputValue : inputValuesForGrowth) {
			List<Object> combinationCandidate = new ArrayList<>(combination);
			combinationCandidate.add(inputValue);
			
			coveredCount = calculateCoveredPairsCount(combinationCandidate);
			
			if(coveredCount > maxCoveredCount) {
				maxCoveredCount = coveredCount;
				selectedInputCombination = combinationCandidate;
			}
		}
		
		if(selectedInputCombination == null) {
			selectedInputCombination = new ArrayList<Object>(combination);
			selectedInputCombination.add(inputValuesForGrowth.get(0));
		}
		
		return selectedInputCombination;
		
	}

	
	
	private int calculateCoveredPairsCount(List<Object> combination) {
	
		int coveredCount = 0;
		
		for (TestPair pair : pairs) {
			if(pair.isCoveredBy(combination)) {
				coveredCount++;
			}
		}
		
		return coveredCount;
		
	}
}
